﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NPOI.HSSF.Record.PivotTable;
using NPOI.SS.Formula.Functions;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Windows;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
using JsonSerializer = System.Text.Json.JsonSerializer;

class Http_Network
{
    HttpClient client;
    
    string apiUrl = "https://dm.delixi-electric.com/api/web/device/electric/meter/statistics?id=1921850272702480386&beginTime=2025-09-01&endTime=2025-09-17";
    public Http_Network()
    {
        var httpClientHandler = new HttpClientHandler();
        httpClientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) =>
        {
            return true;
        };
        client = new HttpClient(httpClientHandler);

    }

    public async Task<List<LoginResponse>> GetDataAsync(string apiGetUrl,string token)
    {
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
        List<LoginResponse> list_logins = new List<LoginResponse>();
        try
        {
            HttpResponseMessage response = await client.GetAsync(apiGetUrl);
            if (response.IsSuccessStatusCode)
            {
                //将 HTTP 内容序列化为字符串的异步操作。
                string responseContent = await response.Content.ReadAsStringAsync();
                //将结果转换成JSON格式
                list_logins = JsonConvert.DeserializeObject<List<LoginResponse>>(responseContent);
            }
            return list_logins;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return list_logins;
        }
    }


    private async Task HttpClientAsync(string apiUrl)
    {
        HttpResponseMessage httpRequest = await client.GetAsync(apiUrl);
        if (httpRequest.IsSuccessStatusCode)
        {
            string contents = await httpRequest.Content.ReadAsStringAsync();
        }
    }

    private async Task PostDataAsyncOne(string apiPostUrl)
    {
        var postData = new {id=0 };
        string jsonData = JsonSerializer.Serialize(postData);
        var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
        HttpResponseMessage response = await client.PostAsync(apiPostUrl, content);
        if (response.IsSuccessStatusCode)
        {
            string responseContent = await response.Content.ReadAsStringAsync();
        }
    }

    public async Task<string> PostDataAsync(string apiPostUrl)
    {
        try
        {
            var postData = new
            {
                Command = "GetUserInfo",
            };
            string jsonData = JsonSerializer.Serialize(postData);
            var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
            HttpResponseMessage response = await client.PostAsync(apiPostUrl, content);
            if (response.IsSuccessStatusCode)
            {
                string responseContent = await response.Content.ReadAsStringAsync();
                return responseContent;
            }
            return "";
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return "";
        }
        
    }


}

public class LoginResponse
{
    public int code { get; set; }
    public string msg { get; set; }
    public LoginData data { get; set; }
    public bool success { get; set; }
}

public class LoginData
{
    public string token { get; set; } 
}


